# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html


from openpyxl import Workbook

class LianjiaspiderPipeline(object):

    def __init__(self):
        self.wb = Workbook()
        self.ws = self.wb.active
        # 设置表头
        self.table = ['url', 'title', 'totle_price', 'price', 'down_paymen', 'monthly_house', 'residential',
                        'area', 'house_type', 'floor_area', 'carpet_area', 'floor', 'renovation', 'elevator','construction_time']

        self.ws.append(self.table)

    def process_item(self, item, spider):
        line = []
        for key in self.table:
            if key in item.keys():
                line.append(item[key])
            else:
                line.append('')
        self.ws.append(line)  # 按行添加
        self.wb.save('lianjia.xlsx')
        return item
